<?php
namespace app\index\model;
use think\Model;

class Evaluate extends Model
{
    /**
     * 根据帖子id 查询该帖子的所有评论
     * @param $card_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function queryAllEvaluateByCardId ($card_id)
    {
        $list = $this->alias("e")
            ->join("fly_user u","e.user_id = u.id")
            ->field(["e.*","u.name username","u.level","u.image"])
            ->where("e.card_id = $card_id")
            ->order("create_time")->paginate($this->showRow);
//            ->fetchSql()

//            ->select();
//        dump($list);die;
        if(empty($list[0])){
            $arr['data']=[];
            $arr['page']=[];
            return $arr;
        }else{
            foreach($list as $key => $val){
                $data[$key] = $val->data;
            }
            $arr['data']=$data;
            $arr['page']=$list->render();
            return $arr;
        }
//        dump($list);die;
    }

    /**
     * 添加帖子评论到 数据库
     * @param $data
     * @return false|int|mixed
     */
    public function addEvaluate ($data)
    {
        $result = $this->save($data);
        if($result > 0){
            return $this->id;
        }else{
            return $result;
        }
    }

    /**
     * 评论某字段 $field 自减 $num
     * @param $field
     * @param $num
     */
    public function setDecEval ($where,$field,$num)
    {
        $this->where($where)->setDec($field,$num);
    }

    /**
     * 评论某字段 $field 自增$num
     * @param $field
     * @param $num
     */
    public function setIncEval ($where,$field,$num)
    {
        $this->where($where)->setInc($field,$num);
    }

    /**
     * 通过用户id 查看他的所有评论
     * @param $user_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function queryAllEvalByUserId($user_id)
    {
        $list = $this->alias("e")
            ->join("fly_card c","c.id=e.card_id")
            ->field(["e.*","c.name"])
            ->where("e.user_id=$user_id")
            ->order("e.create_time")->paginate(4);
//            ->select();
//        dump($list);die;
        if(empty($list[0])){
            $arr['data']=[];
            $arr['page']=[];
            return $arr;
        }else{
            foreach($list as $key => $val){
                $data[$key] = $val->data;
            }
//            dump($data);die;
            $arr['data']=$data;
            $arr['page']=$list->render();
            return $arr;
        }
    }

    /**0
     * 根据$where条件数组,跟新一条评论数据
     * @param $where
     * @param $data
     * @return $this
     */
    public function updateEval ($where,$data)
    {
        return $this->where($where)->update($data);
    }

    /**
     * 根据帖主的id 查询所有评论
     * @param $user_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function queryEvaluateById ($user_id)
    {
        $list = $this->alias("e")
            ->join("fly_card c","e.card_id=c.id")
            ->join("fly_user u","e.user_id=u.id")
            ->field(["c.name","c.sort","u.name username","e.create_time","e.user_id","c.id"])
            ->where("c.user_id = $user_id")
            ->order("e.create_time desc")->paginate($this->showRow);
//            ->select();
        if(empty($list[0])){
            $arr['data']=[];
            $arr['page']=[];
            $arr['total']=0;
            return $arr;
        }else{
            foreach($list as $key => $val){
                $data[$key] = $val->data;
            }
//            dump($data);die;
            $arr['data']=$data;
            $arr['page']=$list->render();
            $arr['total']=$list->total();
            return $arr;
        }
    }
}

?>